import xml.dom.minidom

def create_a_document():
    doc = xml.dom.minidom.Document();
    # create an element
    business_element = doc.createElementNS("http://www.boddie.org.uk/paul/business","business");
    # add element to DOM
    doc.appendChild(business_element);
    return doc, business_element;

def get_a_document(name="mynew.xml"):
    return xml.dom.minidom.parse(name);

def get_a_document_from_file(file_object):
    return xml.dom.minidom.parse(file_object);

def add_a_location(doc, business_element):
    location_element=doc.createElementNS("http://www.boddie.org.uk/paul/business","location");
    business_element.appendChild(location_element);
    return location_element;

def add_surroundings(doc, location_element):
    surroundings_element = doc.createElementNS("http://www.boddie.org.uk/paul/business", "surroundings")
    location_element.appendChild(surroundings_element)
    # create a text node to add to surroundings_element
    description = doc.createTextNode("A quiet, scenic park with lots of wildlife.")
    # add the text node to surroundings_element
    surroundings_element.appendChild(description)
    return surroundings_element

def add_building(doc, location_element):
    building_element = doc.createElementNS("http://www.boddie.org.uk/paul/business", "building")
    location_element.appendChild(building_element)
    return building_element

def name_building(building_element):
    building_element.setAttributeNS("http://www.boddie.org.uk/paul/business", "business:name", "Ivory Tower")

def write_to_screen(doc):
    print doc.toprettyxml();

def write_to_file(doc, name="mynew.xml"):
    fileobj = open(name,"w");
    fileobj.write(doc.toprettyxml());
    fileobj.close();

doc, busy = create_a_document()
loc = add_a_location(doc, busy)
surr = add_surroundings(doc,loc)
build = add_building(doc, loc)
name_building(build)

write_to_screen(doc)
